package com.ims.iaa.swf;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/*
 * Created on 2005-3-2
 *
 */
/**
*
* ByteArrayUtil
*
* This class provides methods to convert int into byte array and
* byte array back into int.
*
*/
public class ByteArrayUtil
{
    public static String toString(byte[] bytData){
        return toString(bytData, 0);
    }
    public static String toString(byte[] bytData, int iStart){
        int iEnd = iStart;
        while(bytData[iEnd] != 0){
            iEnd++;
        }
        int iLen = iEnd - iStart;
        if(iLen == 0){
            return "";
        }
        InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(bytData, iStart, iLen));
        char cData[] = new char[iLen];
        try{
            reader.read(cData);
            return String.valueOf(cData);
        }catch(IOException e){
            return "";
            //it is impossible
        }finally{
            try{reader.close();}catch(IOException e){};
        }
    }
	/**
	*
	* Method converting int into byte array.
	*
	* @param number The int value to be converted.
	*
	*/
	public static byte[] toByteArray(int number)
	{
		int temp = number;
		byte[] bbyData = new byte[4];
		for (int i = 0; i < 4; i++)
		{
			bbyData[i] = new Integer(temp & 0xff).byteValue();
			temp = temp >> 8;
		}
		return bbyData;
	}
	
	/**
	*
	* Method converting byte array into int.
	*
	* @param The byte array to be converted.
	*
	*/
	public static int toInteger(byte[] bytData, int iIndex)
	{
		int iValue = 0;
		int iByte;
		for (int i = 3; i > 0; i--)
		{
			iByte = bytData[i + iIndex];
			if (iByte >= 0){
				iValue = iValue + iByte;
			}else{
				iValue = iValue + 256 + iByte;
			}
			iValue = iValue * 256;
		}
		
		iByte = bytData[iIndex];
		if (iByte < 0){
			iValue = iValue + 256 + iByte;
		}else{
			iValue = iValue + iByte;
		}
		return iValue;
	}
	
	/**
	*
	* Method converting byte array into int.
	*
	* @param The byte array to be converted.
	*
	*/
	public static int toInteger(byte[] bytData)
	{
		int iValue = 0;
		int iByte;
		for (int i = 3; i > 0; i--)
		{
			iByte = bytData[i];
			if (iByte >= 0){
				iValue = iValue + iByte;
			}else{
				iValue = iValue + 256 + iByte;
			}
			iValue = iValue * 256;
		}
		
		iByte = bytData[0];
		if (iByte < 0){
			iValue = iValue + 256 + iByte;
		}else{
			iValue = iValue + iByte;
		}
		return iValue;
	}
	/**
	*
	* Method converting byte array into int.
	*
	* @param The byte array to be converted.
	*
	*/
	public static int toShort(byte[] bytData, int iIndex)
	{
	    int iValue = 0;
	    int iByte = bytData[1 + iIndex];
		if (iByte >= 0){
			iValue = iValue + iByte;
		}else{
			iValue = iValue + 256 + iByte;
		}
		iValue = iValue * 256;
		
		iByte = bytData[iIndex];
		if (iByte < 0){
			iValue = iValue + 256 + iByte;
		}else{
			iValue = iValue + iByte;
		}
		return iValue;
	}
	
	/**
	*
	* Method converting byte array into int.
	*
	* @param The byte array to be converted.
	*
	*/
	public static int toShort(byte[] bytData)
	{
		int iValue = 0;
		int iByte = bytData[1];
		if (iByte >= 0){
			iValue = iValue + iByte;
		}else{
			iValue = iValue + 256 + iByte;
		}
		iValue = iValue * 256;
		
		iByte = bytData[0];
		if (iByte < 0){
			iValue = iValue + 256 + iByte;
		}else{
			iValue = iValue + iByte;
		}
		return iValue;
	}
}

